package com.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hr.domain.dto.HouseFacilityDTO;
import com.hr.domain.po.HouseFacility;
import com.hr.domain.query.HouseFacilityPageParams;
import com.hr.domain.query.HouseFacilityPageResult;
import com.hr.domain.vo.HouseFacilityVO;
import com.hr.service.HouseFacilityService;
import com.hr.mapper.HouseFacilityMapper;
import com.hr.utils.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author 86150
* @description 针对表【house_facility(房源配套设施表)】的数据库操作Service实现
* @createDate 2025-09-24 09:35:56
*/
@Slf4j
@Service
@AllArgsConstructor
public class HouseFacilityServiceImpl implements HouseFacilityService{

    private final HouseFacilityMapper houseFacilityMapper;


    /**
     * 获取房屋配套设施列表
     * @return houseFacilityList
     */
    @Override
    public List<HouseFacilityVO> getHouseFacilityList() {
        List<HouseFacilityVO> houseFacilityList = houseFacilityMapper.getHouseFacilityList();
        if (houseFacilityList == null){
            return null;
        }
        return houseFacilityList;
    }

    /**
     * 分页获取房屋配套设施列表
     * @param queryParams
     * @return houseFacilityPageResult
     */
    @Override
    public HouseFacilityPageResult getHouseFacilityPageList(HouseFacilityPageParams queryParams) {
        //开始分页
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        //传入分页参数
        List<HouseFacilityVO> houseFacilityList = houseFacilityMapper.getHouseFacilityPageList(queryParams);
        //转换为Page<HouseFacilityVO>类型
        Page<HouseFacilityVO> page = (Page<HouseFacilityVO>) houseFacilityList;
        //返回结果
        return new HouseFacilityPageResult(page.getTotal(), page.getResult());
    }

    /**
     * 获取房屋配套设施类型
     * @return houseFacilityTypeList
     */
    @Override
    public List<String> getHouseFacilityType() {
       List<String> houseFacilityTypeList =  houseFacilityMapper.getHouseFacilityType();
        // 使用Stream API去除重复的类型数据
       return houseFacilityTypeList.stream()
               .distinct()
               .collect(Collectors.toList());
    }

    /**
     * 添加房屋配套设施
     * @param houseFacilitydto
     * @return msg
     */
    @Override
    public String addHouseFacility(HouseFacilityDTO houseFacilitydto) {

        HouseFacility houseFacility = new HouseFacility();
        BeanUtils.copyProperties(houseFacilitydto, houseFacility);

        //设置默认值
        //设置默认距离为150米
        houseFacility.setDistance("150");

        int row = houseFacilityMapper.addHouseFacility(houseFacility);
        return row > 0 ? "添加成功" : "添加失败";

    }

    /**
     * 删除房屋配套设施
     * @param id
     * @return msg
     */
    @Override
    public String deleteHouseFacility(Integer id) {
        int row = houseFacilityMapper.deleteHouseFacility(id);
        return row > 0 ? "删除成功" : "删除失败";
    }

    /**
     * 获取单个房屋配套设施
     * @param id
     * @return houseFacility
     */
    @Override
    public HouseFacilityVO getHouseFacility(Integer id) {
        HouseFacilityVO houseFacility = houseFacilityMapper.getHouseFacility(id);
        if (houseFacility == null){
            return null;
        }
        return houseFacility;
    }

    /**
     * 修改房屋配套设施
     * @param houseFacilitydto
     * @return msg
     */
    @Override
    public String updateHouseFacility(HouseFacilityDTO houseFacilitydto) {
        int row = houseFacilityMapper.updateHouseFacility(houseFacilitydto);
        return row > 0 ? "修改成功" : "修改失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteHouseFacilityBatch(List<Integer> ids) {
        // 参数校验
        if (ids == null || ids.isEmpty()) {
            return "未选择任何记录";
        }

        try {
            int deletedRows = houseFacilityMapper.deleteHouseFacilityBatch(ids);

            if (deletedRows != ids.size()) {
                log.error("批量删除不完全，期望删除 {} 条，实际删除 {} 条", ids.size(), deletedRows);
                return "部分删除失败";
            }

            return "批量删除成功";

        } catch (Exception e) {
            log.error("批量删除异常", e);
            throw new RuntimeException("删除失败", e); // 事务会回滚
        }
    }
}




